<?php
/* Template Name: 标签归档
 * Author: Cleris
 */

get_header();

//php获取中文字符拼音首字母
function getFirstCharter($str){
	if(empty($str)) return;
	$fchar=ord($str{0});
	if($fchar>=ord('0')&&$fchar<=ord('9')) return "Num";
	if($fchar>=ord('A')&&$fchar<=ord('z')) return strtoupper($str{0});
	$s1=iconv('UTF-8','gb2312',$str);
	$s2=iconv('gb2312','UTF-8',$s1);
	$s=$s2==$str?$s1:$str;
	$asc=ord($s{0})*256+ord($s{1})-65536;
	if($asc>=-20319&&$asc<=-20284) return 'A';
	if($asc>=-20283&&$asc<=-19776) return 'B';
	if($asc>=-19775&&$asc<=-19219) return 'C';
	if($asc>=-19218&&$asc<=-18711) return 'D';
	if($asc>=-18710&&$asc<=-18527) return 'E';
	if($asc>=-18526&&$asc<=-18240) return 'F';
	if($asc>=-18239&&$asc<=-17923) return 'G';
	if($asc>=-17922&&$asc<=-17418) return 'H';
	if($asc>=-17417&&$asc<=-16475) return 'J';
	if($asc>=-16474&&$asc<=-16213) return 'K';
	if($asc>=-16212&&$asc<=-15641) return 'L';
	if($asc>=-15640&&$asc<=-15166) return 'M';
	if($asc>=-15165&&$asc<=-14923) return 'N';
	if($asc>=-14922&&$asc<=-14915) return 'O';
	if($asc>=-14914&&$asc<=-14631) return 'P';
	if($asc>=-14630&&$asc<=-14150) return 'Q';
	if($asc>=-14149&&$asc<=-14091) return 'R';
	if($asc>=-14090&&$asc<=-13319) return 'S';
	if($asc>=-13318&&$asc<=-12839) return 'T';
	if($asc>=-12838&&$asc<=-12557) return 'W';
	if($asc>=-12556&&$asc<=-11848) return 'X';
	if($asc>=-11847&&$asc<=-11056) return 'Y';
	if($asc>=-11055&&$asc<=-10247) return 'Z';
	return 'Other';
}

//标签文章
function tagPost($tag) {
	global $post;
	$output = '<li><a class="tag-name" href="'.get_tag_link($tag).'">'. $tag->name .'</a><a class="icon-rss" href="'. get_tag_feed_link( $tag->term_id ) . '" rel="external"></a><br>';
	$posts = get_posts( "tag_id=". $tag->term_id ."&numberposts=2" );
	if( $posts ){
		foreach( $posts as $post ) {
			setup_postdata( $post );
			$output.= '<a href="'.get_permalink().'">'.get_the_title().'</a> <small>('.get_the_time('Y-m-d').')</small><br>';
		}
	}
	$output.= '</li>';
	return $output;
}

//导航菜单
	$custom_fields = get_post_custom_keys($post_id);
	if( !in_array( 'pgmenu', $custom_fields) ){
		$pgmenu = 1;
	}
?>
	<div class="hide"><?php ini_map()?></div>
	<div class="main-page" <?php if( $pgmenu!==1 ) echo 'style="padding:20px 25px"'?>>
		<?php if( $pgmenu==1 && !ismobile() ) wp_nav_menu( array('theme_location' => 'pagemenu', 'class' => 'pg-menu') )?>
		<h2 class="entry-title"><?php the_title()?></h2>
<?php while (have_posts()) : the_post()?>
		<span><?php edit_post_link('编辑文章','<i class="icon-edit"></i> ')?></span>
		<div class="context" property="v:description"><?php the_content()?></div>
		<div class="abc-list"><?php
	$abc = array('A','B','C','D','E','F','G','H','J','K','L','M','N','O','P','Q','R','S','T','W','X','Y','Z','Num','Other' );
	for( $i=0; $i<25; $i++ ){
		echo '<a href="#tag-' . $abc[$i] . '" id="abc-' . $abc[$i] . '" class="none">' . $abc[$i] . '</a>';
	}
?></div>
		<div class="tag-list">
			<?php $tags_list = get_tags('orderby=name&order=ASC');
if ($tags_list) {
	foreach($tags_list as $tag) {
		if( getFirstCharter($tag->name) == 'A' ) $Tag_1[] = (object)$tag;
		if( getFirstCharter($tag->name) == 'B' ) $Tag_2[] = (object)$tag;
		if( getFirstCharter($tag->name) == 'C' ) $Tag_3[] = (object)$tag;
		if( getFirstCharter($tag->name) == 'D' ) $Tag_4[] = (object)$tag;
		if( getFirstCharter($tag->name) == 'E' ) $Tag_5[] = (object)$tag;
		if( getFirstCharter($tag->name) == 'F' ) $Tag_6[] = (object)$tag;
		if( getFirstCharter($tag->name) == 'G' ) $Tag_7[] = (object)$tag;
		if( getFirstCharter($tag->name) == 'H' ) $Tag_8[] = (object)$tag;
		if( getFirstCharter($tag->name) == 'J' ) $Tag_9[] = (object)$tag;
		if( getFirstCharter($tag->name) == 'K' ) $Tag_10[] = (object)$tag;
		if( getFirstCharter($tag->name) == 'L' ) $Tag_11[] = (object)$tag;
		if( getFirstCharter($tag->name) == 'M' ) $Tag_12[] = (object)$tag;
		if( getFirstCharter($tag->name) == 'N' ) $Tag_13[] = (object)$tag;
		if( getFirstCharter($tag->name) == 'O' ) $Tag_14[] = (object)$tag;
		if( getFirstCharter($tag->name) == 'P' ) $Tag_15[] = (object)$tag;
		if( getFirstCharter($tag->name) == 'Q' ) $Tag_16[] = (object)$tag;
		if( getFirstCharter($tag->name) == 'R' ) $Tag_17[] = (object)$tag;
		if( getFirstCharter($tag->name) == 'S' ) $Tag_18[] = (object)$tag;
		if( getFirstCharter($tag->name) == 'T' ) $Tag_19[] = (object)$tag;
		if( getFirstCharter($tag->name) == 'W' ) $Tag_20[] = (object)$tag;
		if( getFirstCharter($tag->name) == 'X' ) $Tag_21[] = (object)$tag;
		if( getFirstCharter($tag->name) == 'Y' ) $Tag_22[] = (object)$tag;
		if( getFirstCharter($tag->name) == 'Z' ) $Tag_23[] = (object)$tag;
		if( getFirstCharter($tag->name) == 'Num' ) $Tag_24[] = (object)$tag;
		if( getFirstCharter($tag->name) == 'Other' ) $Tag_25[] = (object)$tag;
	}
	for( $i=1; $i<26; $i++ ){
		$A = 'Tag_';
		$B = $A.$i;
		$new_tags_list = $$B;
		if($new_tags_list){
			$g = 0;
			foreach ($new_tags_list as $tag) {
				$g++;
				$match = getFirstCharter($tag->name);
				if ($g==1) echo '<ul id="tag-' . $match . '" class="tag-order"><strong>' . $match . '</strong>';
				echo tagPost($tag);
			}
			echo '</ul>';
		}
	}
}
wp_reset_query();
?>
		</div>
<?php if('open'== $post->comment_status) comments_template()?>
<?php endwhile?>
	</div>
<script type="text/javascript">
	jQuery(document).ready(function($){
		$(".tag-order").each(function(){
			var tagID = this.id;
			abcID = tagID.replace("tag","#abc");
			$(abcID).removeAttr("class");
		});
	});
</script>
<?php get_footer()?>